/**
 * 创建人:  @author huangpeng
 * 创建时间:  2024年10月02日 01:15
 * 项目名称:  lemon-blog-cloud
 * 文件名称:  ElasticsearchController
 * 文件描述:  @Description: Elasticsearch相关接口
 * 公司名称:  xxxxxx
 * <p>
 * All rights Reserved, Designed By xxxxxx
 *
 * @Copyright: 2021-2024
 */
package com.lemon.search.controller;

import com.lemon.common.core.utils.StringUtils;
import com.lemon.common.core.web.controller.BaseController;
import com.lemon.common.core.web.domain.AjaxResult;
import com.lemon.common.core.web.page.TableDataInfo;
import com.lemon.search.service.ElasticSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

/**
 * 包名称：com.lemon.search.controller
 * 类名称：ElasticsearchController
 * 类描述：Elasticsearch相关接口
 * 创建人：@author huangpeng
 * 创建时间：2024年10月02日 01:15
 */
@Api(value = "ElasticSearch相关接口", tags = {"ElasticSearch相关接口"})
@RestController
@RequestMapping("/search")
public class ElasticsearchController extends BaseController {

    @Autowired
    private ElasticSearchService searchService;

    /**
     * 项目启动时，初始化文章数据到elasticsearch
     * @Title: init
     * @Description: 项目启动时，初始化文章数据到elasticsearch
     * @return: void
     * @throws:
     * @author: huangpeng
     * @Date: 2024/10/7 22:10
     */
    @PostConstruct
    public void init() {
        this.initElasticSearchIndex(null);
        logger.info("ElasticSearch初始化索引成功");
    }


    /**
     * 通过ElasticSearch搜索文章
     * @Title: searchBlog
     * @Description: 通过ElasticSearch搜索文章
     * @param keyWord 关键字
     * @param pageNum 页码
     * @param pageSize 页大小
     * @return: String
     * @throws:
     * @author: huangpeng
     * @Date: 2024/10/2 00:58
     */
    @ApiOperation(value = "通过ElasticSearch搜索文章", notes = "通过ElasticSearch搜索文章", response = String.class)
    @GetMapping("/elasticSearchBlog")
    public TableDataInfo searchBlog(@RequestParam(required = false) String keyWord,
                                    @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                    @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        keyWord = StringUtils.isNotEmpty(keyWord) ? QueryParser.escape(keyWord) : "";
        return searchService.searchToElasticsearch(keyWord, pageNum, pageSize);
    }

    /**
     * 通过ids批量删除ElasticSearch文章索引
     * @Title: deleteElasticSearchByIds
     * @Description: 通过ids批量删除ElasticSearch文章索引
     * @param ids 文章id，多个逗号隔开
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2024/10/2 00:59
     */
    @ApiOperation(value = "通过ids批量删除ElasticSearch文章索引", notes = "通过ids批量删除ElasticSearch文章索引", response = String.class)
    @PostMapping("/deleteElasticSearchByIds")
    public AjaxResult deleteElasticSearchByIds(@RequestParam(value = "ids") String ids) {
        searchService.deleteElasticSearchByIds(ids);
        return AjaxResult.success("通过ids批量删除ElasticSearch文章索引成功");
    }

    /**
     * 通过文章id删除ElasticSearch文章索引
     * @Title: deleteElasticSearchById
     * @Description: 通过文章id删除ElasticSearch文章索引
     * @param id 文章id
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2024/10/2 01:00
     */
    @ApiOperation(value = "通过文章id删除ElasticSearch文章索引", notes = "通过文章id删除ElasticSearch文章索引", response = String.class)
    @PostMapping("/deleteElasticSearchById")
    public AjaxResult deleteElasticSearchById(@RequestParam() String id) {
        searchService.deleteElasticSearchById(id);
        return AjaxResult.success("通过文章id删除ElasticSearch文章索引成功");
    }

    /**
     * 把文章添加到Elasticsearch索引中
     * @Title: addElasticSearchIndexById
     * @Description: 把文章添加到Elasticsearch索引中
     * @param id 文章id
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2024/10/2 01:00
     */
    @ApiOperation(value = "ElasticSearch通过文章Id添加索引", notes = "添加文章", response = String.class)
    @PostMapping("/addElasticSearchIndexById")
    public AjaxResult addElasticSearchIndexById(@RequestParam(required = true) Long id) {
        searchService.addElasticSearchIndexById(id);
        return AjaxResult.success("ElasticSearch通过文章Id添加索引成功");
    }

    /**
     * 初始化Elasticsearch索引
     * @Title: initElasticSearchIndex
     * @Description: 初始化Elasticsearch索引
     * @return: AjaxResult
     * @param request 请求头
     * @throws:
     * @author: huangpeng
     * @Date: 2024/10/2 01:01
     */
    @ApiOperation(value = "ElasticSearch初始化索引", notes = "ElasticSearch初始化索引", response = String.class)
    @PostMapping("/initElasticSearchIndex")
    public AjaxResult initElasticSearchIndex(HttpServletRequest request) {
        searchService.initElasticSearchIndex(request);
        return AjaxResult.success("ElasticSearch初始化索引成功");
    }
}
